// eslint-disable-next-line import/named
import apiClient from '@/api/apiClient';
import { PermissionSearchForm } from '@/pages/management/system/permission/permission-search-form';

import { Permission, Rule } from '#/entity';

export enum PermissionApi {
  List = '/security/permissions',
  Permission = '/security/permission',
  RuleList = '/security/permission/',
}

const PermissionList = (searchForm: PermissionSearchForm) =>
  apiClient.get({ url: PermissionApi.List, params: searchForm });

const rolePermissionList = (roleId: number) =>
  apiClient.get<Permission[]>({ url: `${PermissionApi.List}/by/${roleId}` });

const ruleList = (permissionId: number, data?: any) =>
  apiClient.get<Rule[]>({ url: `${PermissionApi.RuleList}/${permissionId}/rules`, params: data });

const createPermission = (data: any) =>
  apiClient.post({
    url: PermissionApi.Permission,
    data,
  });

const deletePermission = (id: number) =>
  apiClient.post({
    url: `${PermissionApi.Permission}/${id}/delete`,
    params: { confirmationText: 'PERMISSION' },
  });

const updatePermission = (roleId: number, data: any) =>
  apiClient.post({
    url: `${PermissionApi.Permission}/${roleId}`,
    data,
  });

const roleAssign = (id: number, data: any) =>
  apiClient.post({
    url: `${PermissionApi.Permission}/${id}/assign`,
    data,
  });

const Logout = () => apiClient.post({ url: '/user/sign_out' });

export default {
  PermissionList,
  Logout,
  ruleList,
  createPermission,
  updatePermission,
  deletePermission,
  rolePermissionList,
  roleAssign,
};
